<?php

namespace app\api\service\impl;

use app\api\model\Student;
use app\api\service\StudentService;

class StudentServiceImpl implements StudentService
{

    public function getAllStudent(): bool|string
    {
        // TODO: Implement getAllStudent() method.
        return SuccessJsonResult(Student::select());
    }

    public function getStudentById(): bool|string
    {
        // TODO: Implement getStudentById() method.
        $id = input('id') ?: null;
        return SuccessJsonResult(Student::find($id));
    }

    public function addStudent(): bool|string
    {
        // TODO: Implement addStudent() method.
        $studentParam = [
            'name' => input('name') ?: null,
            'sex' => input('sex') ?: null,
            'birth' => input('birth') ?: null,
            'department' => input('department') ?: null,
            'address' => input('address') ?: null
        ];
        $student = new Student();
        $student->save($studentParam);
        return CustomJsonResult(code: 200, msg: '添加成功');
    }

    public function updateStudent(): bool|string
    {
        // TODO: Implement updateStudent() method.
        $studentParam = [
            'id' => input('id') ?: null,
            'name' => input('name') ?: null,
            'sex' => input('sex') ?: null,
            'birth' => input('birth') ?: null,
            'department' => input('department') ?: null,
            'address' => input('address') ?: null
        ];
        $student = Student::find($studentParam['id']);
        $student->save($studentParam);
        return CustomJsonResult(code: 200, msg: '更新成功');
    }

    public function deleteStudent(): bool|string
    {
        // TODO: Implement deleteStudent() method.
        $id = input('id') ?: null;
        Student::destroy($id);
        return CustomJsonResult(code: 200, msg: '删除成功');
    }
}